public class LinkedList {
    class Node{
        int val;
        Node next;
        public Node(){
            head=null;
        }
        public Node(int data) {
            this.val=data;
        }
    }
    public Node head;

    public void createList(){
        Node node = new Node(4);
        head=node;
    }
    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        node.next=head;
        head=node;
    }
    //尾插法
    public void addLast(int data){
        if(head==null){
            Node node = new Node(data);
            head=node;
            return;
        }
        Node node = new Node(data);
        Node cur = head;
        while(cur.next != null){
            cur=cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    private Node findIndex(int index) {
        Node cur = head;
        while((index-1)!=0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    private void checkIndex(int index) throws IndexException{
        if(index<0 || index > size()){
            throw new IndexException("下标不合法");
        }
    }

    public void addIndex(int index,int data) throws IndexException{
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        Node node = new Node(data);
        Node cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;

    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head==null){
            return false;
        }
        Node cur = head;
        while (cur != null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        Node cur = head;
        while (cur.next!= null){
            if(cur.next.val == key){
                cur.next=cur.next.next;
            }else {
                cur = cur.next;
            }
        }
    }
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        Node prev = head;
        Node cur = head.next;
        while(cur != null){
            while(cur!=null && cur.val == key) {
                cur = cur.next;
            }
            prev.next=cur;
            prev=cur;
            if(cur!=null){
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }

    }

    //得到单链表的长度
    public int size(){
        int count=0;
        Node cur = head;
        while (cur != null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    public void clear() { }
    public void display() {
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
}
